22. Quiz: Code Your Own Filter

Filters

To really understand how kernel convolution works, it's best to get some hands-on practice. In this code quiz, you're encouraged to test a variety of filter values and click Test Run to see how they affect the original image!

In this quiz, you also have a choice of images to read in, which you can do by changing the file name in imread to one of these three files:

ice_cream_truck.jpg
brain_MR.jpg
city_hall.jpg

Start Quiz:

import matplotlib.pyplot as plt
import numpy as np
import cv2

# Read in the image
image = cv2.imread('ice_cream_truck.jpg')
# Convert to RGB (from BGR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# ---------------------------------------------------------- #

## TODO: Convert the image to grayscale for filtering

## TODO: Create a custom kernel
## Change the values in this empty kernel
## You can even try making a 5x5 kernel
kernel = np.array([[ 0, 0, 0], 
                   [ 0, 0, 0], 
                   [ 0, 0, 0]])

## TODO: Filter the grayscale image (from the first TODO) using filter2D
## Change the below None code
filtered_image = None


# ---------------------------------------------------------- #
if(filtered_image is not None):
    plt.imshow(filtered_image, cmap='gray')